package org.smile.auth.session;

import org.smile.commons.NotImplementedException;
import org.smile.expression.AddSymbolExpression;

import java.beans.PropertyChangeListener;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public abstract class AbstractManager<T extends Session> implements Manager<T>{

    private Map<String,Session> sessions=new ConcurrentHashMap<>();
    protected  SessionIdGenerator idGenerator;

    protected Store<T> store;

    private  int maxInactiveInterval=60*60;

    public void setStore(Store<T> store){
        this.store=store;
    }

    @Override
    public SessionIdGenerator getSessionIdGenerator() {
        return this.idGenerator;
    }

    @Override
    public void setSessionIdGenerator(SessionIdGenerator sessionIdGenerator) {
        this.idGenerator=sessionIdGenerator;
    }

    @Override
    public long getSessionCounter() {
        return -1;
    }

    @Override
    public void setSessionCounter(long sessionCounter) {

    }

    @Override
    public int getMaxActive() {
        return 0;
    }

    @Override
    public void setMaxActive(int maxActive) {

    }

    @Override
    public int getActiveSessions() {
        return 0;
    }

    @Override
    public long getExpiredSessions() {
        return 0;
    }

    @Override
    public void setExpiredSessions(long expiredSessions) {

    }

    @Override
    public int getRejectedSessions() {
        return 0;
    }

    @Override
    public int getSessionMaxAliveTime() {
        return this.maxInactiveInterval;
    }

    @Override
    public void setSessionMaxAliveTime(int sessionMaxAliveTime) {
        this.maxInactiveInterval=sessionMaxAliveTime;
    }

    @Override
    public int getSessionAverageAliveTime() {
        return 0;
    }

    @Override
    public int getSessionCreateRate() {
        return 0;
    }

    @Override
    public int getSessionExpireRate() {
        return 0;
    }

    @Override
    public void add(Session session) {
        this.sessions.put(session.getId(),session);
    }

    @Override
    public void addPropertyChangeListener(PropertyChangeListener listener) {

    }

    @Override
    public void changeSessionId(Session session, String newId) {

    }

    @Override
    public Session createSession() {
       return this.createSession(this.idGenerator.generate());
    }


    @Override
    public Session[] findSessions() {
        return new Session[0];
    }

    @Override
    public void load() throws ClassNotFoundException, IOException {
        throw new NotImplementedException();
    }

    @Override
    public void remove(Session session) {

    }

    @Override
    public Session findSession(String id)  {
        Session session=sessions.get(id);
        return session;
    }

    @Override
    public void remove(Session session, boolean update) {
        sessions.remove(session.getId());
        session.expire();
    }

}
